package com.sanqing.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sanqing.dao.Dao;

public class MapPz {
    private static Map<Integer, String> IdToName = new HashMap<>();
    private static Map<String, Integer> NameToId = new HashMap<>();
    static {
        loadFromDatabase();
    }
    
    private static void loadFromDatabase() {
        try {
            List<BookType> list = Dao.selectBookCategory();
            if (list != null) {
                for (BookType bookType : list) {
                    if (bookType != null && bookType.getId() != null && bookType.getTypeName() != null) {
                    	IdToName.put(bookType.getId(), bookType.getTypeName());
                    	NameToId.put(bookType.getTypeName(), bookType.getId());
                    	
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("加载图书类型数据失败: " + e.getMessage());
            e.printStackTrace();
            // 可以考虑设置默认值或记录错误日志
        }
    }

    public static Map<Integer, String> getIdToName() {
        return IdToName;
    }
    public static Map<String, Integer> getNameToId() {
        return NameToId;
    }
    
    public static void refresh() {
        synchronized (MapPz.class) { // 添加同步以避免多线程问题
        	IdToName.clear();
            loadFromDatabase();
        }
    }
}